#include "ipc.h"

/* IPC */
void IPC_Task(void)
{
    #ifndef MATLAB_MEX_FILE

    // Continue processing messages
    uint32_t command;
    command = IpcRegs.IPCRECVCOM;
    switch (command)
    {
    case IPC_SET_BITS_16:
        IPCLiteRtoLSetBits(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_SET_BITS_32:
        IPCLiteRtoLSetBits(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_CLEAR_BITS_16:
        IPCLiteRtoLClearBits(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_CLEAR_BITS_32:
        IPCLiteRtoLClearBits(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_DATA_WRITE_16:
        IPCLiteRtoLDataWrite(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_DATA_WRITE_32:
        IPCLiteRtoLDataWrite(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_SET_BITS_16_PROTECTED:
        IPCLiteRtoLSetBits_Protected(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_SET_BITS_32_PROTECTED:
        IPCLiteRtoLSetBits_Protected(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_CLEAR_BITS_16_PROTECTED:
        IPCLiteRtoLClearBits_Protected(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_CLEAR_BITS_32_PROTECTED:
        IPCLiteRtoLClearBits_Protected(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_DATA_WRITE_16_PROTECTED:
        IPCLiteRtoLDataWrite_Protected(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_DATA_WRITE_32_PROTECTED:
        IPCLiteRtoLDataWrite_Protected(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_DATA_READ_16:
        IPCLiteRtoLDataRead(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_DATA_READ_32:
        IPCLiteRtoLDataRead(IPC_FLAG0, IPC_FLAG31);
        break;
    case IPC_FUNC_CALL:
        IPCLiteRtoLFunctionCall(IPC_FLAG0, IPC_FLAG31);
        break;
    default:
        //ErrorFlag = 1;
        break;
    }

    // IPC Lite Driver Functions acknowledge the IPC interrupt.
    // There is no need to ACK the IPC interrupt flag separately.
    //
    IpcRegs.IPCACK.bit.IPC0 = 1;

    #endif
}
uint32_t usRWord32 = 11;
uint32_t usRWord32_1 = 12;
uint32_t usRWord32_2 = 13;

void IPC_FLG0_ISR(void)
{
    #ifndef MATLAB_MEX_FILE

//	my_IPCLiteLtoRDataRead(IPC_FLAG0, 0x3fbea, IPC_LENGTH_32_BITS, IPC_FLAG31);

    IPCLiteLtoRDataWrite(IPC_FLAG0, 0x3fbf0,(uint32_t)usRWord32_1,
                         IPC_LENGTH_32_BITS,
                         IPC_FLAG31);
   while (IPCLiteLtoRGetResult(&usRWord32_1,IPC_LENGTH_32_BITS,
                               IPC_FLAG31) != STATUS_PASS)
   {
   }
    ////////////////
    IPCLiteLtoRDataWrite(IPC_FLAG0, 0x3fbea,(uint32_t)usRWord32_2,
                         IPC_LENGTH_32_BITS,
                         IPC_FLAG31);
   while (IPCLiteLtoRGetResult(&usRWord32_2,IPC_LENGTH_32_BITS,
                               IPC_FLAG31) != STATUS_PASS)
   {
   }
    ////////////////
    
    while (IPCLiteLtoRDataRead(IPC_FLAG0, 0x3fbea, IPC_LENGTH_32_BITS,
                               IPC_FLAG31) != STATUS_PASS)
    {
    }

   // Result of Read will be read into usRWord16 variable.
   while (IPCLiteLtoRGetResult(&usRWord32,IPC_LENGTH_32_BITS,
                               IPC_FLAG31) != STATUS_PASS)
   {
   }
    //IpcRegs.IPCACK.bit.IPC0 = 1;
    #endif
}

uint16_t my_IPCLiteLtoRGetResult (void *pvData, uint16_t usLength, uint32_t ulStatusFlag)
{
    uint16_t status = 0;
    #ifndef MATLAB_MEX_FILE
    status = IPCLiteLtoRGetResult (pvData, usLength, ulStatusFlag);
    #endif
    return status;
}

uint16_t my_IPCLiteLtoRDataRead(uint32_t ulFlag, uint32_t ulAddress, uint16_t usLength,
                                    uint32_t ulStatusFlag)
{
   uint16_t status = 0;
   #ifndef MATLAB_MEX_FILE
   status = IPCLiteLtoRDataRead(ulFlag, ulAddress, usLength,
                                    ulStatusFlag);
   #endif
   return status;
}

uint16_t my_IPCRtoLFlagBusy (uint32_t ulFlags)
{
#ifndef MATLAB_MEX_FILE
    IPCRtoLFlagBusy (ulFlags);
#endif
}

uint16_t my_IPCLtoRFlagBusy (uint32_t ulFlags)
{
#ifndef MATLAB_MEX_FILE
    IPCLtoRFlagBusy (ulFlags);
#endif
}

void my_IPCLtoRFlagSet (uint32_t ulFlags)
{
#ifndef MATLAB_MEX_FILE
    IPCLtoRFlagSet (ulFlags);
#endif
}

void my_IPCLtoRFlagClear (uint32_t ulFlags)
{
#ifndef MATLAB_MEX_FILE
    IPCLtoRFlagClear (ulFlags);
#endif
}

/*
uint16_t my_IPCLiteLtoRSetBits(uint32_t ulFlag, uint32_t ulAddress, uint32_t ulMask,
                                   uint16_t usLength,uint32_t ulStatusFlag);
uint16_t my_IPCLiteLtoRSetBits_Protected (uint32_t ulFlag, uint32_t ulAddress, uint32_t ulMask,
                                              uint16_t usLength,uint32_t ulStatusFlag);
uint16_t my_IPCLiteLtoRClearBits(uint32_t ulFlag, uint32_t ulAddress, uint32_t ulMask,
                                     uint16_t usLength,uint32_t ulStatusFlag);
uint16_t my_IPCLiteLtoRClearBits_Protected (uint32_t ulFlag, uint32_t ulAddress, uint32_t ulMask,
                                                uint16_t usLength,uint32_t ulStatusFlag);
uint16_t my_IPCLiteLtoRDataWrite(uint32_t ulFlag, uint32_t ulAddress, uint32_t ulData,
                                     uint16_t usLength,uint32_t ulStatusFlag);
uint16_t my_IPCLiteLtoRDataWrite_Protected(uint32_t ulFlag, uint32_t ulAddress, uint32_t ulData,
                                               uint16_t usLength,uint32_t ulStatusFlag);
uint16_t my_IPCLiteLtoRFunctionCall(uint32_t ulFlag, uint32_t ulAddress, uint32_t ulParam,
                                        uint32_t ulStatusFlag);
uint16_t my_IPCLiteReqMemAccess (uint32_t ulFlag, uint32_t ulMask, uint16_t ulMaster,
                                     uint32_t ulStatusFlag);
void my_IPCLiteRtoLDataRead(uint32_t ulFlag, uint32_t ulStatusFlag);
void my_IPCLiteRtoLSetBits(uint32_t ulFlag, uint32_t ulStatusFlag);
void my_IPCLiteRtoLSetBits_Protected (uint32_t ulFlag, uint32_t ulStatusFlag);
void my_IPCLiteRtoLClearBits(uint32_t ulFlag, uint32_t ulStatusFlag);
void my_IPCLiteRtoLClearBits_Protected (uint32_t ulFlag, uint32_t ulStatusFlag);
void my_IPCLiteRtoLDataWrite(uint32_t ulFlag, uint32_t ulStatusFlag);
void my_IPCLiteRtoLDataWrite_Protected(uint32_t ulFlag, uint32_t ulStatusFlag);
void my_IPCLiteRtoLFunctionCall(uint32_t ulFlag, uint32_t ulStatusFlag);
*/
